05. Thymeleaf - Loop
035ND C01 L03 A08 THYMELEAF LOOP
Instructions
Create a model directory and add a User class.
public class User {
public Integer id;
public String name;
public Integer age;
public User(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
}
Add a new method in our controller
@RequestMapping("demo2")
public String demo2(Model model) {
List<User> lst = new ArrayList<>();
lst.add(new User(1, "Tom", 30));
lst.add(new User(2, "Jerry", 29));
lst.add(new User(3, "Nancy", 27));
model.addAttribute("list", lst);
return "demo2";
}
Copy demo.html and paste as demo2.html. Remove everything between body and /body tags. And add the following
<h3>Thymeleaf Loop</h3>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<tr th:each="user : ${list}" >
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
</tr>
</table>
Demo
035ND C01 L03 A09 THYMELEAF LOOP EXAMPLE